package org.bigdata.framework.core.utils;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class VideoUtil {
	 /**
     * 第五帧的图片
     * jpg格式
     * @throws Exception
     */
    public static String getVideoFirstImg(String path,String imagePath)throws Exception {
    	File desFile = new File(imagePath);
        if (!desFile.getParentFile().exists()) {
            desFile.mkdirs();
        }
        Frame frame = null;
       //构造器支持InputStream，可以直接传MultipartFile.getInputStream()
        FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(path);
        //开始播放
        fFmpegFrameGrabber.start();
        //获取视频总帧数
        int ftp = fFmpegFrameGrabber.getLengthInFrames();
        //指定第几帧
        fFmpegFrameGrabber.setFrameNumber(5);
        //获取指定第几帧的图片
        frame = fFmpegFrameGrabber.grabImage();
        //文件绝对路径+名字
        String fileName = imagePath;
        File outPut = new File(fileName);
        ImageIO.write(FrameToBufferedImage(frame), "jpg", outPut);
        fFmpegFrameGrabber.close();
        return fileName;
    }
    /**
     * 倒数第五帧的图片
     * jpg格式
     * @throws Exception
     */
    public static String getVideoLastImg(String path,String imagePath)throws Exception {
        Frame frame = null;
        FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(path);
        //开始播放
        fFmpegFrameGrabber.start();
        //获取视频总帧数
        int ftp = fFmpegFrameGrabber.getLengthInFrames();
        //指定第几帧
        fFmpegFrameGrabber.setFrameNumber(ftp - 5);
        //获取指定第几帧的图片
        frame = fFmpegFrameGrabber.grabImage();
        String fileName = imagePath;
        File outPut = new File(fileName);
        ImageIO.write(FrameToBufferedImage(frame), "jpg", outPut);
        fFmpegFrameGrabber.close();
        return fileName;
    }

    public static BufferedImage FrameToBufferedImage(Frame frame) {
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bufferedImage = converter.getBufferedImage(frame);
        return bufferedImage;
    }

    public static void main(String[] args) throws Exception{
        String videoFileName = "C:/Users/ninemax/Desktop/694847-518a3baf6096388726e4e9ac7ca0336f.mp4";
        System.out.println(getVideoLastImg(videoFileName,"d:/"));
    }

}